package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.CompanyType;
import ex.com.jm.food.service.AdminCompanyTypeService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

/**
 * 企业类型管理action
 * @author zhaojw
 *
 */
public class AdminCompanyTypeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private AdminCompanyTypeService adminCompanyTypeService;
	private CompanyType companyType;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String comtypeName;
	private Integer comtypeId;
	private Integer status;
	private String order;
	private String sort;
	public AdminCompanyTypeService getAdminCompanyTypeService() {
		return adminCompanyTypeService;
	}
	public void setAdminCompanyTypeService(
			AdminCompanyTypeService adminCompanyTypeService) {
		this.adminCompanyTypeService = adminCompanyTypeService;
	}
	public CompanyType getCompanyType() {
		return companyType;
	}
	public void setCompanyType(CompanyType companyType) {
		this.companyType = companyType;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getComtypeName() {
		return comtypeName;
	}
	public void setComtypeName(String comtypeName) {
		this.comtypeName = comtypeName;
	}
	public Integer getComtypeId() {
		return comtypeId;
	}
	public void setComtypeId(Integer comtypeId) {
		this.comtypeId = comtypeId;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}

	/**
	 * 到达列表	
	 * @return
	 */
	public String comtypeList(){
		return "comtypeList";
	}
	/**
	 * 得到数据
	 */
	public void getComtypes(){
		 HttpServletResponse response = ServletActionContext.getResponse();  
	     response.setContentType("text/plain;charset=utf-8"); 
	     int offset=(initPage-1)*pageRow;
	     List<CompanyType> companyTypes=this.getAdminCompanyTypeService().getCompanyTypes(offset, pageRow, comtypeName, status, order, sort);
	     totalCount=this.getAdminCompanyTypeService().getcomTypeCount(comtypeName, status);
	     JSONArray jocomtype=JSONArray.fromObject(companyTypes);
	     JSONObject jo=JsonUtils.buildJson(jocomtype, initPage, pageRow, totalCount);
			PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(jo);
			out.print(jo);
			out.flush();
			out.close();
	}
	/**
	 * 获取某一个类型信息
	 * @return
	 */
	public String editComtype(){
		if(comtypeId!=null){
			companyType=this.getAdminCompanyTypeService().editCompanyType(comtypeId);
		}
		return "edit";
	}
	/**
	 * 保存更新信息
	 * @return
	 */
	public String saveComtype(){
		if(companyType.getComtypeName()==null||companyType.getComtypeName().equals("")){
			addFieldError("companyType.comTypeName", "类型名称不能为空");
			return "edit";
		}
		CompanyType checkName=this.getAdminCompanyTypeService().checkName(companyType.getComtypeName());
		if(checkName!=null&&companyType.getComtypeId()==null){
			addFieldError("companyType.comTypeName", "类型名称已存在");
			return "edit";
		}
		if(companyType.getComtypeId()!=null&&!companyType.getComtypeId().equals("")){
			this.getAdminCompanyTypeService().updateComtype(companyType);
			return "comtypeList";
		}
		companyType.setCreated(DateUtils.getCurrentDate());
		companyType.setStatus(1);
		this.getAdminCompanyTypeService().saveComType(companyType);
		return "comtypeList";
	}
	/**
	 * 验证
	 */
	public void checkName(){
		HttpServletResponse response = ServletActionContext.getResponse();  
	     response.setContentType("text/plain;charset=utf-8"); 
		CompanyType checkName=this.getAdminCompanyTypeService().checkName(comtypeName);
		String msg="";
		if(checkName!=null&&comtypeId==null){
			msg="类型名称已存在";
		}else{
			msg="类型名称可用";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 更新状态
	 * @return
	 */
	public String updateStatus(){
		boolean flag=this.getAdminCompanyTypeService().updateStatus(status, comtypeId);
		if(flag==true){
			return "comtypeList";
		}
		return "comtypeList";
	}
}
